package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.Entity;
import com.zw.yzk.kabu.data.network.task.AuthCoserTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.management.ToastManager;
import com.zw.yzk.kabu.view.view.AuthCoserView;

import javax.inject.Inject;

/**
 * Created by zhanwei on 2017/7/6.
 */

public class AuthCoserPresenter implements Presenter {

    private UseCase authCoser;
    private AppCompatActivity activity;
    private AuthCoserView authCoserView;


    @Inject
    public AuthCoserPresenter(UseCase authCoser, AppCompatActivity activity) {
        this.authCoser = authCoser;
        this.activity = activity;
    }

    public void setView(AuthCoserView view) {
        this.authCoserView = view;
    }

    public void auth(String name, String id) {
        authCoser.execute(new AuthCoserSubscriber(activity), new AuthCoserTask(name, id));
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        authCoser.unSubscribe();
    }

    private class AuthCoserSubscriber extends BaseSubscriber<Entity> {

        public AuthCoserSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.auth_success);
        }

        @Override
        public void onNext(Entity entity) {
            super.onNext(entity);
            ToastManager.getInstance().showToast(activity, activity.getResources().getString(R.string.auth_success));
            authCoserView.authSuccess();
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

    }
}
